<template>
<div class="ui  comments">
  <div class="comment" v-for="(item,i) in comments" :key="i">
    <a class="avatar">
      <div class="name" v-if="!item.avatar">{{item.username[0]}}</div>
      <img :src="item.avatar" alt="" v-else>
    </a>
    <div class="content">
      <a class="author">{{item.username}}</a>
      <div class="metadata">
        <p class="date">{{timeago.format(item.createTime, 'zh_CN')}}</p>
      </div>
      <div class="text">
        <p>{{item.content}}</p>
      </div>
      <div class="actions" @click="reply(item.id)">
        <a class="reply">回复</a>
      </div>
    </div>
    <div class="comments" v-for="(ele,j) in item.children" :key="j">
      <div class="comment">
        <a class="avatar">
          <div class="name" v-if="!item.avatar">{{ele.username[0]}}</div>
          <img :src="item.avatar" alt="" v-else>
        </a>
        <div class="content">
          <a class="author">{{ele.username}}<span>@{{ele.parent.username}}</span></a>
          <div class="metadata">
            <p class="date">{{timeago.format(ele.createTime, 'zh_CN')}}</p>
          </div>
          <div class="text">{{ele.content}}</div>
          <div class="actions" @click="reply(ele.id)">
            <a class="reply" >回复</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import * as timeago from "timeago.js";
export default {
  props:['comments'],
  data(){
    return {
      timeago
    }
  },
  methods:{
    reply(id){
      this.$emit('reply',id)
    }
  }
};
</script>

<style lang="scss" scoped>
.name{
    border-radius: 50%;
    background-color: #d4e3fc;
    color: #0052d9;
    height: 2.5em;
    line-height: 2.5em;
    text-align: center;
}
.author{
  span{
    color: #0aaa6a;
    font-weight: 600;
  }
  .img{
    height: 2.5em;
  }
}
.ui.comments .comment .avatar img, .ui.comments .comment img.avatar{
  border-radius: 50%;
}
.date{
  font-size: 12px;
}
</style>
